﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _1065010_1065053_1065059_1065064
{
    public partial class DSNhanVien : Form
    {
        public DSNhanVien()
        {
            InitializeComponent();
        }

        //private Boolean co;
        //private void Lock(Boolean f)
        //{
        //    //manv.Enabled = f;
        //    tennv.Enabled = f;
        //    matk.Enabled = f;
        //    cbxloainv.Enabled = f;
        //}

        //private void LockButton(Boolean f)
        //{
        //    button1.Enabled = f;
        //    button2.Enabled = f;
        //    button3.Enabled = f;
        //    button4.Enabled = f;
        //}

        string chuoiload = "select * from NhanVien";
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void DSNhanVien_Load(object sender, EventArgs e)
        {
        //    Lock(false);
        //    LockButton(true);
        
            dgvdsnv.DataSource = DataProvider.HienThiDanhSach(chuoiload);
            string sql = "select LoaiNV, TenLoaiNV from LoaiNhanVien";
            SqlConnection kn = DataProvider.ConnectionData();
            SqlCommand lenh = new SqlCommand(sql, kn);
            SqlDataReader doc = lenh.ExecuteReader();
            List<LoaiNhanVien> dsloainv = new List<LoaiNhanVien>();
            while (doc.Read())
            {
                LoaiNhanVien loainv = new LoaiNhanVien();
                loainv.loainv = doc.GetString(0);
                loainv.tenloainv = doc.GetString(1);
                dsloainv.Add(loainv);
                //MessageBox.Show(loainv.maloainv );
            }
            
            cbxloainv.DataSource = dsloainv;
            cbxloainv.ValueMember = "TenLoaiNV";
            cbxloainv.DisplayMember = "LoaiNV";
            
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //co = true ;
            //Lock(true );
            //LockButton(false );
            if (manv != null)
            {
                SqlConnection kn = DataProvider.ConnectionData();
                string sql = "Insert Into NhanVien(MaNV,TenNV,LoaiNV,MaTK) Values (@MaNV, @TenNV,@LoaiNV,@MaTK)";
                SqlCommand lenh = new SqlCommand(sql, kn);

                NhanVien nv = new NhanVien();
                nv.manv = manv.Text;
                nv.tennv = tennv.Text;
                nv.loainv = cbxloainv.Text;
                nv.matk = matk.Text;

                SqlParameter para1 = new SqlParameter("@MaNV", nv.manv);
                lenh.Parameters.Add(para1);
                SqlParameter para2 = new SqlParameter("@TenNV", nv.tennv);
                lenh.Parameters.Add(para2);
                SqlParameter para3 = new SqlParameter("@LoaiNV", nv.loainv);
                lenh.Parameters.Add(para3);
                SqlParameter para4 = new SqlParameter("@MaTK", nv.matk);
                lenh.Parameters.Add(para4);
                lenh.ExecuteNonQuery();
                dgvdsnv.DataSource = DataProvider.HienThiDanhSach(chuoiload);
                kn.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string manv = dgvdsnv.CurrentRow.Cells[0].Value.ToString();
            SqlConnection kn = DataProvider.ConnectionData();
            string sql = "delete from NhanVien where MaNV=@MaNV";
            SqlCommand lenh = new SqlCommand(sql, kn);
            SqlParameter para = new SqlParameter("MaNV", SqlDbType.VarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = manv ;
            lenh.Parameters.Add(para);
            lenh.ExecuteNonQuery();
            dgvdsnv.Rows.Remove(dgvdsnv.CurrentRow);
            dgvdsnv.Refresh();
            dgvdsnv.DataSource = DataProvider.HienThiDanhSach(chuoiload);
            kn.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }
}
